﻿CREATE TABLE [CashSale].[datCashSaleDetails] (
    [DetailsID]      INT             IDENTITY (1, 1) NOT NULL,
    [CashSaleID]     INT             NOT NULL,
    [ProductID]      INT             NOT NULL,
    [Quantity]       DECIMAL (11, 2) NOT NULL,
    [Price]          DECIMAL (11, 2) NOT NULL,
    [RabattPercent1] DECIMAL (11, 2) NOT NULL,
    [RabattPercent2] DECIMAL (11, 2) NOT NULL,
    [RabattFix]      DECIMAL (11, 2) NOT NULL,
    [SalePrice]      AS              ((([Price]*[Quantity])*((1)-[RabattPercent1]/(100)))*((1)-[RabattPercent2]/(100))-[RabattFix]) PERSISTED,
    [TaxCode]        TINYINT         CONSTRAINT [DF_datCashSaleDetails_TaxCode] DEFAULT ((2)) NOT NULL,
    [TaxRate]        DECIMAL (11, 2) CONSTRAINT [DF_datCashSaleDetails_TaxRate] DEFAULT ((0.2)) NOT NULL,
    CONSTRAINT [PK_datCashSaleDetails] PRIMARY KEY CLUSTERED ([DetailsID] ASC),
    CONSTRAINT [FK_datCashSaleDetails_datCashSales] FOREIGN KEY ([CashSaleID]) REFERENCES [CashSale].[datCashSale] ([CashSaleID]),
    CONSTRAINT [FK_datCashSaleDetails_datProducts] FOREIGN KEY ([ProductID]) REFERENCES [Products].[datProducts] ([ProductID])
);


GO
CREATE NONCLUSTERED INDEX [datCashSaleDetails_CashSaleID]
    ON [CashSale].[datCashSaleDetails]([CashSaleID] ASC);

